home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / getpass.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  191 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Utilities to get a password and/or the current user name.
  5.  
  6. getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
  7. getuser() - Get the user name from the environment or password database.
  8.  
  9. GetPassWarning - This UserWarning is issued when getpass() cannot prevent
  10.                  echoing of the password contents while reading.
  11.  
  12. On Windows, the msvcrt module will be used.
  13. On the Mac EasyDialogs.AskPassword is used, if available.
  14.  
  15. '''
  16. import os
  17. import sys
  18. import warnings
  19. __all__ = [
  20.     'getpass',
  21.     'getuser',
  22.     'GetPassWarning']
  23.  
  24. class GetPassWarning(UserWarning):
  25.     pass
  26.  
  27.  
  28. def unix_getpass(prompt = 'Password: ', stream = None):
  29.     """Prompt for a password, with echo turned off.
  30.  
  31.     Args:
  32.       prompt: Written on stream to ask for the input.  Default: 'Password: '
  33.       stream: A writable file object to display the prompt.  Defaults to
  34.               the tty.  If no tty is available defaults to sys.stderr.
  35.     Returns:
  36.       The seKr3t input.
  37.     Raises:
  38.       EOFError: If our input tty or stdin was closed.
  39.       GetPassWarning: When we were unable to turn echo off on the input.
  40.  
  41.     Always restores terminal settings before returning.
  42.     """
  43.     fd = None
  44.     tty = None
  45.     
  46.     try:
  47.         fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
  48.         tty = os.fdopen(fd, 'w+', 1)
  49.         input = tty
  50.         if not stream:
  51.             stream = tty
  52.     except EnvironmentError:
  53.         e = None
  54.         
  55.         try:
  56.             fd = sys.stdin.fileno()
  57.         except:
  58.             passwd = fallback_getpass(prompt, stream)
  59.  
  60.         input = sys.stdin
  61.         if not stream:
  62.             stream = sys.stderr
  63.         
  64.     except:
  65.         stream
  66.  
  67.     if fd is not None:
  68.         passwd = None
  69.         
  70.         try:
  71.             old = termios.tcgetattr(fd)
  72.             new = old[:]
  73.             new[3] &= ~(termios.ECHO)
  74.             
  75.             try:
  76.                 termios.tcsetattr(fd, termios.TCSADRAIN, new)
  77.                 passwd = _raw_input(prompt, stream, input = input)
  78.             finally:
  79.                 termios.tcsetattr(fd, termios.TCSADRAIN, old)
  80.  
  81.         except termios.error:
  82.             e = None
  83.             if passwd is not None:
  84.                 raise 
  85.             passwd is not None
  86.             del input
  87.             del tty
  88.             passwd = fallback_getpass(prompt, stream)
  89.         except:
  90.             None<EXCEPTION MATCH>termios.error
  91.         
  92.  
  93.     None<EXCEPTION MATCH>termios.error
  94.     stream.write('\n')
  95.     return passwd
  96.  
  97.  
  98. def win_getpass(prompt = 'Password: ', stream = None):
  99.     '''Prompt for password with echo off, using Windows getch().'''
  100.     if sys.stdin is not sys.__stdin__:
  101.         return fallback_getpass(prompt, stream)
  102.     import msvcrt as msvcrt
  103.     for c in prompt:
  104.         msvcrt.putch(c)
  105.     
  106.     pw = ''
  107.     while None:
  108.         c = msvcrt.getch()
  109.         if c == '\r' or c == '\n':
  110.             break
  111.         
  112.         if c == '\x03':
  113.             raise KeyboardInterrupt
  114.         if c == '\x08':
  115.             pw = pw[:-1]
  116.             continue
  117.         pw = pw + c
  118.         continue
  119.         msvcrt.putch('\r')
  120.         msvcrt.putch('\n')
  121.         return pw
  122.  
  123.  
  124. def fallback_getpass(prompt = 'Password: ', stream = None):
  125.     warnings.warn('Can not control echo on the terminal.', GetPassWarning, stacklevel = 2)
  126.     if not stream:
  127.         stream = sys.stderr
  128.     
  129.     print >>stream, 'Warning: Password input may be echoed.'
  130.     return _raw_input(prompt, stream)
  131.  
  132.  
  133. def _raw_input(prompt = '', stream = None, input = None):
  134.     if not stream:
  135.         stream = sys.stderr
  136.     
  137.     if not input:
  138.         input = sys.stdin
  139.     
  140.     prompt = str(prompt)
  141.     if prompt:
  142.         stream.write(prompt)
  143.         stream.flush()
  144.     
  145.     line = input.readline()
  146.     if not line:
  147.         raise EOFError
  148.     line
  149.     if line[-1] == '\n':
  150.         line = line[:-1]
  151.     
  152.     return line
  153.  
  154.  
  155. def getuser():
  156.     '''Get the username from the environment or password database.
  157.  
  158.     First try various environment variables, then the password
  159.     database.  This works on Windows as long as USERNAME is set.
  160.  
  161.     '''
  162.     import os
  163.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  164.         user = os.environ.get(name)
  165.         if user:
  166.             return user
  167.     
  168.     import pwd as pwd
  169.     return pwd.getpwuid(os.getuid())[0]
  170.  
  171.  
  172. try:
  173.     import termios
  174.     (termios.tcgetattr, termios.tcsetattr)
  175. except (ImportError, AttributeError):
  176.     
  177.     try:
  178.         import msvcrt
  179.     except ImportError:
  180.         
  181.         try:
  182.             from EasyDialogs import AskPassword
  183.         except ImportError:
  184.             getpass = fallback_getpass
  185.  
  186.         getpass = AskPassword
  187.  
  188.     getpass = win_getpass
  189.  
  190. getpass = unix_getpass
  191.